package com.xinyue.user.controller;

import java.io.File;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.xinyue.user.dao.IdentificationMapper;
import com.xinyue.user.pojo.Identification;
import com.xinyue.user.pojo.User;
import com.xinyue.user.service.IdentificationService;
import com.xinyue.user.utils.FileUtils;
import com.xinyue.user.utils.JsonResult;

@RestController
@RequestMapping("/identify")
public class IdentificationController {
	@Autowired
	private IdentificationService identificationService; 
	
	@ResponseBody
   @RequestMapping("/test")
   public String test(Identification identification){
		try {
			identificationService.insert(identification);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "ok";
   }
	@RequestMapping("/add")
	public JsonResult add(Identification identification,@RequestParam(name = "pic1",required = false) MultipartFile file,
			@RequestParam(name = "pic2",required = false) MultipartFile file1){
		String pic2=FileUtils.getRandomFileName(file1);
		String pic1=FileUtils.getRandomFileName(file);
		try {
			file.transferTo(new File("D:/upload/img/"+pic1));
			file.transferTo(new File("D:/upload/img/"+pic2));
			int insert = identificationService.insert(identification);
			return new JsonResult(200,insert,"设置正常！");
		} catch (Exception e) {
			e.printStackTrace();
		}
		 return new JsonResult(201,null,"设置异常！");
   }
	@RequestMapping("/selectByUid")
	public JsonResult selectByUid(Integer uid){
		try {
			Identification identification = identificationService.selectByUid(uid);
			return new JsonResult(200,identification,"设置正常！");
		} catch (Exception e) {
			e.printStackTrace();
		}
		 return new JsonResult(201,null,"设置异常！");
   }
	@RequestMapping("/selectAll")
	public JsonResult selectAll(){
		try {
			List<Identification> identifications = identificationService.selectAll();
			return new JsonResult(200,identifications,"设置正常！");
		} catch (Exception e) {
			e.printStackTrace();
		}
		 return new JsonResult(201,null,"设置异常！");
   }
	@RequestMapping("/deleteByUid")
	public JsonResult deleteByUid(Integer uid){
		try {
			//Identification identification = identificationService.selectByUid(uid);
		//	return new JsonResult(200,identification,"设置正常！");
		} catch (Exception e) {
			e.printStackTrace();
		}
		 return new JsonResult(201,null,"设置异常！");
   }
}
